package main

import (
	"GORM_Demo04/part08/MyPojo"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

/*	常用方法

	【10】Update:更新数据
*/

func main() {
	db, err := gorm.Open("mysql", "root:123456@tcp(192.168.11.189:3306)/lztest?charset=utf8&parseTime=True&loc=Local")

	if err != nil {
		panic(err)
	}
	defer db.Close()

	db.LogMode(true)

	//【10】Update:更新数据

	var user MyPojo.User
	/*	//SELECT * FROM `users`  WHERE (uid = 1) ORDER BY `users`.`uid` ASC LIMIT 1
		db.First(&user, "uid = ?", 1)*/

	//SELECT * FROM `users`  WHERE (uid = 1)
	//db.Find(&user, "uid = ?", 1)
	//fmt.Println(user)

	//db.Model(&user).Update("u_name", "哈哈")

	//db.Where("uid = ?", 1).First(&user).Update("u_name", "我的名字")
	//db.Where("uid = ?", 1).Find(&user).Update("u_name", "我的名字")

	//正序更新
	//	db.Where("u_name = ?", "开开").First(&user).Update("u_name", "我的名字")

	//逆序更新
	//UPDATE `users` SET `u_name` = '哈哈'  WHERE `users`.`uid` = 1 AND ((u_name = '我的名字'))
	db.Where("u_name = ?", "开开").Find(&user).Update("u_name", "哈哈")

}
